Imports iConverter.Extensions
Imports System.IO
Imports System.Diagnostics


''' <summary>
''' 发送到 Foobar插件
''' </summary>
''' <remarks></remarks>
Public Class SendToFoobar
    Inherits Send(Of My.MySettings)

    Public Sub New()
        MyBase.New(New Guid("AF5FC5EA-93DA-460F-8DB1-6685177E178F"),
                  "发送到Foobar插件",
                  "提供将当前文件添加到 Foobar 播放列表中的功能",
                  My.Resources.foobar_32x32, My.Resources.foobar_16x16)

      '  Me.AppPath = Me.Settings.FoobarPath

    End Sub


    ''' <summary>
    ''' Foobar 的程序路径
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property AppPath As String


    Protected Overrides Function CheckRun() As Boolean
        Return CheckAppPath()
    End Function

    Protected Overrides Sub OnSending(doc As iConverter.Contracts.IConvertDocument)
        '"d:\Foobar2000\foobar2000.exe" /add "%1"

        If Me.CheckAppPath Then
            Dim arguments = String.Format(" /add  ""{0}""  ", doc.FileName)

            Process.Start(Me.AppPath, arguments)

        End If
    End Sub


    ''' <summary>
    ''' 从注册表中获取程序路径
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetFoobarAppPathFromReg() As String
        'HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\foobar2000
        'InstallDir

        Try
            Dim value As String = TryCast(My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\foobar2000", "InstallDir", String.Empty), String)

            If String.IsNullOrEmpty(value) Then
                value = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\foobar2000", "InstallDir", String.Empty)
            End If

            If String.IsNullOrEmpty(value) Then
                Return String.Empty
            End If

            Dim path = IO.Path.Combine(value, "foobar2000.exe")

            If IO.File.Exists(path) Then
                Return path
            Else
                Return String.Empty
            End If

        Catch ex As Exception

        End Try
        Return String.Empty
    End Function

    ''' <summary>
    ''' 检测程序路径是否可用
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CheckAppPath() As Boolean

        If String.IsNullOrEmpty(Me.AppPath) Then
            Me.AppPath = Me.Settings.FoobarPath
        End If

        If String.IsNullOrEmpty(Me.AppPath) Then
            Me.AppPath = GetFoobarAppPathFromReg()
        End If

        If Not String.IsNullOrEmpty(Me.AppPath) Then
            Return File.Exists(Me.AppPath)
            
        End If
        Return False
    End Function


End Class